$(function () {
  $(".drop-box").each(function (e) {
    $(this).css("z-index", $(".drop-box").length - e)
  }), $("#order .name").click(function () {
    if (jQuery.cookie("user_token")) {
      var s = this.className.replace(/name/, "").replace(/\s+/g, ""),
        e = $(this),
        t = (e.children("i"), e.next(".drop-list"), "<ul>"),
        a = [],
        r = 0;
      if (e.parents(".form-item").hasClass("selected")) return e.parents(".form-item").removeClass("selected"), !1;
      switch (s) {
        case "city_name":
          a = cityArray.map(function (e) {
            return e.n
          });
          break;
        case "warehouse_name":
          if (!(r = e.attr("data-sid")) && 0 != r) return $(".mask").show(), dialog({
            content: '<div class="pop-tit"><span>提示信息</span><span class="close" id="pop-close" data-id="ok" title="关闭"></span></div><div class="order-bounced">请先选择城市！</div>',
            width: "380",
            height: "130",
            fixed: !0,
            okValue: "确定",
            ok: function () {
              $(".mask").hide()
            }
          }).show(), !1;
          a = cityArray[parseInt(r)].s;
          break;
        case "breed_name":
          a = breedsArray;
          break;
        case "way_name":
          a = wayArray
      }
      $.each(a, function (e, a) {
        t += "warehouse_name" === s ? '<li data-id="' + a.i + '" data-cid="' + e + '" data-sid="' + r + '">' + a.n + "</li>" : '<li data-id="' + a.i + '" data-sid="' + e + '">' + a.n + "</li>"
      }), t += "</ul>", e.parents(".form-item").eq(0).addClass("selected").find(".drop-list").html(t).end().siblings(".selected").removeClass("selected")
    } else $(".mask").show(), dialog({
      title: "提示信息",
      content: '<div class="hint">您未登录，请登陆后操作！</div>',
      width: "380",
      height: "80",
      fixed: !0,
      okValue: "确定",
      ok: function () {
        location.href = "login.htm"
      },
      cancelValue: "取消",
      cancel: function () {
        $(".mask").hide()
      }
    }).show()
  }), $(".drop-list").on("click", "li", function (e) {
    var a = $(this),
      e = e.delegateTarget.className.replace(/drop-list/, "").replace(/\s+/g, ""),
      s = a.text(),
      t = a.data("id");
    switch (a.parents(".form-item").removeClass("selected").find(".title").text(s), e) {
      case "city_box":
        $("#citysId").val(t), $("#citysName").val(s), $("#warehouseId").val(""), $("#warehouseName").val(""), $(".warehouse_name").attr("data-sid", a.data("sid")).find(".title").text("请选择仓库");
        break;
      case "warehouse_box":
        $("#warehouseId").val(t), $("#warehouseName").val(s);
        break;
      case "breed_box":
        $("#breedId").val(t), $("#breedName").val(s);
        break;
      case "way_box":
        $("#wayId").val(t), $("#wayName").val(s)
    }
  }), $(document).on("click", function (e) {
    var e = $(e.target),
      a = $("#order .selected");
    e.hasClass("selected") || a.has(e).length || a.removeClass("selected")
  }), $("#order").validator({
    fields: {
      citysId: "required",
      warehouseId: "required",
      breedName: "required",
      wayId: "required"
    },
    invalidClass: "text-error",
    msgWrapper: "span",
    msgMaker: function (e) {
      return '<span class="' + e.type + '"></span>'
    },
    valid: function (e) {
      $("#citysId").val(), $("#citysName").val(), $("#warehouseId").val(), $("#warehouseName").val(), $("#breedName").val(), $("#wayId").val();
      var a = {
        cityId: $("#citysId").val(),
        cityName: $("#citysName").val(),
        warehouseId: $("#warehouseId").val(),
        warehouseName: $("#warehouseName").val(),
        breedName: $("#breedName").val(),
        processMethod: $("#wayId").val()
      };
      $.ajax({
        type: "GET",
        url: "saveOrder.html",
        contentType: "application/json",
        dataType: "jsonp",
        jsonp: "callbackparam",
        jsonpCallback: "success_jsonpCallback",
        data: "jsonParamStr=" + encodeURI(JSON.stringify(a)),
        success: function (e) {
          "1" == e.resultCode ? window.location.href = "success.html" : alert(e.resultInfo)
        }
      })
    }
  })
});
